//
//  DatePickerViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/4.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "DatePickerViewController.h"
#import "UIButton+Dzm.h"
#import "NSDate+WQCalendarLogic.h"

@interface DatePickerViewController () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIButton *cancelBtn;

- (IBAction)cancelBtnAction:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UIButton *okBtn;

- (IBAction)okbtnAction:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UIView *dateContentView;
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;

@property (strong, nonatomic) NSMutableArray *hourList;
@property (strong, nonatomic) NSMutableArray *minuteList;


@property (strong, nonatomic) NSMutableArray *selectedTime;

- (IBAction)bgTap:(UITapGestureRecognizer *)sender;

@property (assign, nonatomic) NSInteger pickRow;

@end

@implementation DatePickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.okBtn     styleButton];
    [self.cancelBtn style1Button];
    
    [self.pickerView setValue: [[UIView alloc ] init] forKeyPath:@"_topLineView"];
    [self.pickerView setValue: [[UIView alloc ] init] forKeyPath:@"_bottomLineView"];
    
    [self initDataSource];
    [self.pickerView reloadAllComponents];
    
    self.view.backgroundColor = [UIColor clearColor];
}

-(void)initDataSource{
    
    self.hourList   = [[NSMutableArray alloc ] init];
    self.minuteList = [[NSMutableArray alloc ] init];
    self.selectedTime=[[NSMutableArray alloc ] init];
    
  
    
    for (NSInteger i = 1; i <= 24; i ++) {
       
        [self.hourList addObject:[self addZero:i]];
    }
 
    [self.pickerView reloadComponent:0];
    
    for (NSInteger i = 0; i <= 60; i ++) {
        
        [self.minuteList addObject:[self addZero:i]];
    }
    [self.pickerView reloadComponent:1];
    [self selectedLastTime];
}


-(NSString *)addZero:(NSInteger )time{
    NSString *hourString = @(time).stringValue;
    if (time < 10) {
        hourString = [NSString stringWithFormat:@"0%@",hourString];
    }
    return hourString;
}

-(void)selectedLastTime{
    if (self.requiredSelectedTime) {
        NSDate *requiredSelectedDate = [NSDate dateFromString:self.requiredSelectedTime format:@"HH:mm"];
        
        NSInteger currentHour = [requiredSelectedDate getCurrentHour];
        for (NSInteger i = 0; i < self.hourList.count; i ++) {
            if ([[self addZero:currentHour] isEqualToString:self.hourList[i]]) {
                [self.pickerView selectRow:i inComponent:0 animated:NO];
                [self.selectedTime addObject:self.hourList[i]];
                break;
            }
        }
        
        NSInteger currentMinute = [requiredSelectedDate getCurrentMinute];
        for (NSInteger i = 0; i < self.minuteList.count; i ++) {
            if ([[self addZero:currentMinute] isEqualToString:self.minuteList[i]]) {
                [self.pickerView selectRow:i inComponent:1 animated:NO];
                [self.selectedTime addObject:self.minuteList[i]];
                break;
            }
        }
    }else{
        [self.selectedTime addObject:@"08"];
        [self.pickerView selectRow:7 inComponent:0 animated:NO];
        [self.selectedTime addObject:@"00"];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   return 16384;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 25;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    switch (component) {
        case 0:
        {
            if (_hourList.count == 0) {
                return @"";
            }
            return _hourList[row%_hourList.count];
        }
        default:
        {
            if (_minuteList.count == 0) {
                return @"";
            }
            return _minuteList[row%_minuteList.count];
        } break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
        {
            NSString *hour = _hourList[row%_hourList.count];
            [self.selectedTime replaceObjectAtIndex:0 withObject:hour];
        }
            break;
        default:
        {
            NSString *minute = _minuteList[row%_minuteList.count];
            [self.selectedTime replaceObjectAtIndex:1 withObject:minute];
        }
            break;
    }
}

/**
 *  取消
 *
 *  @param sender 取消
 */
- (IBAction)cancelBtnAction:(UIButton *)sender {
    self.cancelBlock();
}

/**
 *  ok
 *
 *  @param sender 按钮
 */
- (IBAction)okbtnAction:(UIButton *)sender {
    self.ensureBlock(self.selectedTime);
}

- (IBAction)bgTap:(UITapGestureRecognizer *)sender {
    self.cancelBlock();
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
